using BoxBinary.AspectCache.Core.Broadcast;
using BoxBinary.AspectCache.Core.Helpers;
using BoxBinary.AspectCache.Core.PolicyDefinition;

namespace BoxBinary.AspectCache.Core
{
    /// <summary>
    /// Provides helper methods for the serialization and deserialization of caching objects.
    /// </summary>
    public static class SerializationHelper
    {
        /// <summary>
        /// Deserializes an <see cref="IPolicyBoundCachePackage"/> from disk.
        /// </summary>
        /// <param name="absoluteFilePath"></param>
        /// <returns></returns>
        public static IPolicyBoundCachePackage DeserializeCacheItemPolicy(string absoluteFilePath)
        {
            return Serialization.DeserializeFromFile(absoluteFilePath) as IPolicyBoundCachePackage;
        }

        /// <summary>
        /// Deserializes a <see cref="BroadcastEvent"/> from disk.
        /// </summary>
        /// <param name="absoluteFilePath"></param>
        /// <returns></returns>
        public static BroadcastEvent DeserializeCacheBroadcastEvent(string absoluteFilePath)
        {
            return Serialization.DeserializeFromFile(absoluteFilePath) as BroadcastEvent;
        }

        /// <summary>
        /// Serializes a <see cref="PolicyBoundCachePackage"/> to disk.
        /// </summary>
        /// <param name="storeRootDir"></param>
        /// <param name="pathForSerialization"></param>
        /// <param name="policyBoundCachePackage"></param>
        /// <param name="fullPathForSerialization">The full path to the file where the object was serialized.</param>
        public static void SerializeCacheItemPolicyToFileSystem(string storeRootDir, string pathForSerialization,
                                                                IPolicyBoundCachePackage policyBoundCachePackage,
                                                                out string fullPathForSerialization)
        {
            Serialization.SerializeToFile(storeRootDir, pathForSerialization, policyBoundCachePackage,
                                          out fullPathForSerialization);
        }
    }
}